package com.lingnan.wanxiangxinxun.service.Impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lingnan.wanxiangxinxun.constant.redisConstant;
import com.lingnan.wanxiangxinxun.entity.po.NewsCategory;
import com.lingnan.wanxiangxinxun.entity.vo.common.PageVO;
import com.lingnan.wanxiangxinxun.mapper.NewsCategoryMapper;
import com.lingnan.wanxiangxinxun.service.INewsCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 新闻分类表 服务实现类
 * </p>
 *
 * @author swl
 * @since 2025-03-13
 */
@Service
@Slf4j
public class NewsCategoryServiceImpl extends ServiceImpl<NewsCategoryMapper, NewsCategory> implements INewsCategoryService {

    @Autowired
    private NewsCategoryMapper newsCategoryMapper;
    @Autowired
    private RedisTemplate<String, String> redisTemplate_String;

    //查找--根据id查询
    @Override
    public List<NewsCategory> getNewsCategoryListByIds(List<Integer> ids) {
        if (ids == null || ids.isEmpty()){
            return null;
        }
        return newsCategoryMapper.getNewsCategoryListByIds(ids);
    }


    @Override
    public PageVO<NewsCategory> getAllNewsCategoryInPage(long current, long size) {
        Page<NewsCategory> page = page(new Page<>(current, size));
        return PageVO.of(newsCategoryMapper.selectPage(page,null));
    }

    // 获取各个新闻类型的浏览量
    @Override
    public Map<NewsCategory, Integer> getViewNumOfVariousNews() {
        long start = System.currentTimeMillis();
        //1.获取所有新闻的id
        List<NewsCategory> newsCategories = newsCategoryMapper.selectList(null);

        //2.获取今天，昨天以及前天的日期
        LocalDate today = LocalDate.now();
        LocalDate yesterday = today.minusDays(1);
        LocalDate dayBeforeYesterday = today.minusDays(2);

        //3.统计各个类型的新闻的浏览量
        List<String> redisKeys = new ArrayList<>();
        for (NewsCategory nc : newsCategories) {
            Integer newsType = nc.getId();
            redisKeys.add(redisConstant.VARIOUS_TYPES_NEWS_VIEW_NUM_KEY + newsType + ":" + today);
            redisKeys.add(redisConstant.VARIOUS_TYPES_NEWS_VIEW_NUM_KEY + newsType + ":" + yesterday);
            redisKeys.add(redisConstant.VARIOUS_TYPES_NEWS_VIEW_NUM_KEY + newsType + ":" + dayBeforeYesterday);
        }
        // 批量获取所有键值
        List<String> viewNums = redisTemplate_String.opsForValue().multiGet(redisKeys);

        Map<NewsCategory, Integer> map = new HashMap<>();
        // 处理结果
        int index = 0;
        for (NewsCategory nc : newsCategories) {
            String todayViewNum = viewNums.get(index++) != null ? viewNums.get(index-1) : "0";
            String yesterdayViewNum = viewNums.get(index++) != null ? viewNums.get(index-1) : "0";
            String dayBeforeYesterdayViewNum = viewNums.get(index++) != null ? viewNums.get(index-1) : "0";

            //该类型的新闻的总浏览量
            Integer total = Integer.parseInt(todayViewNum) +
                    Integer.parseInt(yesterdayViewNum) +
                    Integer.parseInt(dayBeforeYesterdayViewNum);
            map.put(nc, total);
        }

        long end = System.currentTimeMillis();
        //log.info("获取各个新闻类型的浏览量，耗时：{}", end - start);
        //4.返回数据
        return map;
    }

}
